<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of StatisticController
 *
 * @author Admin
 */
class Back_StatisticController extends Zend_Controller_Action 
{
    public function init() {
        $this->_helper->layout->setLayout('admin');
    }
    
    public function indexAction() {
        
    }
    
    public function allstatisticAction() {
        $statistic = new Back_Model_Statistic();
        
        $active = 1; $notConfirmed = 2; $blocked = 3;
        /*
         * Advertisers
         */        
        $countAdvertisers = $statistic->getCountAllAdvertisers();
        $this->view->countAdvertisers = $countAdvertisers;
        
        $countActiveAdvertisers = $statistic->getCountStatusAdvertisrs($active);
        $this->view->countActiveAdvertisers = $countActiveAdvertisers;
        
        $countNotConfirmedAdvertisers = $statistic->getCountStatusAdvertisrs($notConfirmed);
        $this->view->countNotConfirmedAdvertisers = $countNotConfirmedAdvertisers;
        
        $countBlockedAdvertisers = $statistic->getCountStatusAdvertisrs($blocked);
        $this->view->countBlockedAdvertisers = $countBlockedAdvertisers;                
        
        /**
         * Advertisements
         */
        $banner = 1; $context = 2; $blog = 3; $socialNetwork = 4;
        $video = 5;
        $countAdvertisements = $statistic->getCountAllAdvertisements();
        $this->view->countAdvertisements = $countAdvertisements;
        
        $countBannerAdvertisements = $statistic->getCountTypeAdvertisements($banner);
        $this->view->countBannerAdvertisements = $countBannerAdvertisements;
        
        $countContextAdvertisements = $statistic->getCountTypeAdvertisements($context);
        $this->view->countContextAdvertisements = $countContextAdvertisements;
        
        $countBlogAdvertisements = $statistic->getCountTypeAdvertisements($blog);
        $this->view->countBlogAdvertisements = $countBlogAdvertisements;
        
        $countSocialNetworkAdvertisements = $statistic->getCountTypeAdvertisements($socialNetwork);
        $this->view->countSocialNetworkAdvertisements = $countSocialNetworkAdvertisements;
        
        $countVideoAdvertisements = $statistic->getCountTypeAdvertisements($video);
        $this->view->countVideoAdvertisements = $countVideoAdvertisements;
        
        /**
         * Campaigns
         */
        $countCampaigns = $statistic->getCountAllCampaigns();
        $this->view->countCampaigns = $countCampaigns;
        
        $countActiveCampaigns = $statistic->getCountStatusCampaigns($active);
        $this->view->countActiveCampaigns = $countActiveCampaigns;
        
        $countNotConfirmedCampaigns = $statistic->getCountStatusCampaigns($notConfirmed);
        $this->view->countNotConfirmedCampaigns = $countNotConfirmedCampaigns;
        
        $countBlockedCampaigns = $statistic->getCountStatusCampaigns($blocked);
        $this->view->countBlockedCampaigns = $countBlockedCampaigns;
        
        /**
         * Publishers
         */
        $countPublishers = $statistic->getCountAllPublishers();
        $this->view->countPublishers = $countPublishers;
        
        $countActivePublishers = $statistic->getCountStatusPublishers($active);
        $this->view->countActivePublishers = $countActivePublishers;
        
        $countNotConfirmedPublishers = $statistic->getCountStatusPublishers($notConfirmed);
        $this->view->countNotConfirmedublishers = $countNotConfirmedPublishers;
        
        $countBlockedPublishers = $statistic->getCountStatusPublishers($blocked);
        $this->view->countBlockedPublishers = $countBlockedPublishers;
        
        /**
         * Publisher websites
         */
        $countSites = $statistic->getCountAllSites();
        $this->view->countSites = $countSites;
        
        $countActiveSites = $statistic->getCountStatusSites($active);
        $this->view->countActiveSites = $countActiveSites;
        
        $countNotConfirmedSites = $statistic->getCountStatusSites($notConfirmed);
        $this->view->countNotConfirmedSites = $countNotConfirmedSites;
        
        $countBlockedSites = $statistic->getCountStatusSites($blocked);
        $this->view->countBlockedSites = $countBlockedSites;
        
        /**
         * Advertising spaces
         */
        $countAdvSpaces = $statistic->getCountAllAdvSpaces();
        $this->view->countAdvSpaces = $countAdvSpaces;
        
        $countActiveAdvSpaces = $statistic->getCountStatusAdvSpaces($active);
        $this->view->countActiveAdvSpaces = $countActiveAdvSpaces;
        
        $countNotConfirmedAdvSpaces = $statistic->getCountStatusAdvSpaces($notConfirmed);
        $this->view->countNotConfirmedAdvSpaces = $countNotConfirmedAdvSpaces;
        
        $countBlockedAdvSpaces = $statistic->getCountStatusAdvSpaces($blocked);
        $this->view->countBlockedAdvSpaces = $countBlockedAdvSpaces;
        
        /**
         * Balance
         */
        $advertisersBalance = $statistic->getAdvertisersBalance();
        $this->view->advertisersBalance = $advertisersBalance;
        
        $publishersBalance = $statistic->getPublishersBalance();
        $this->view->publishersBalance = $publishersBalance;
        
        $totalBalance = $advertisersBalance['balance'] + $publishersBalance['balance'];
        $this->view->totalBalance = round($totalBalance, 2);     
    }
    
    public function financestatisticAction() {
        $statistic = new Back_Model_Statistic();
        
        $active = 1; $notConfirmed = 2; $blocked = 3;
        
        /**
         * System statistic         
         */
        $banner = 1; $adver = 2; $blog = 3; $social = 4; $video = 5;
        $currentDate = Zend_Date::now();
        $day = intval($currentDate->get(Zend_Date::DAY));
        $month = intval($currentDate->get(Zend_Date::MONTH));
        $year = intval($currentDate->get(Zend_Date::YEAR));
        $turnover = array();
        for ($i = 0; $i <= $day; $i++) {
            $dayTurnover = $statistic->getSelectDayTurnover($i, $month, $year);            
            $dayBannerTurnover = $statistic->getSelectAdvTypeDayTurnover($i, $month, $year, $banner);
            $dayAdverTurnover = $statistic->getSelectAdvTypeDayTurnover($i, $month, $year, $adver);
            $dayVideoTurnover = $statistic->getSelectAdvTypeDayTurnover($i, $month, $year, $video);
            $dayContextTurnover = $dayAdverTurnover['sum'] + $dayVideoTurnover['sum'];
            if ($dayTurnover['date'] != null) {                  
                $turnover[$i]['sum'] = $dayTurnover['sum'];
                $turnover[$i]['date'] = $dayTurnover['date'];
                $turnover[$i]['banner_turnover'] = $dayBannerTurnover['sum'];
                $turnover[$i]['context_turnover'] = $dayContextTurnover;
            }
        }
        $this->view->turnover = $turnover;
        /**
         * Balance
         */
        $advertisersBalance = $statistic->getAdvertisersBalance();
        $this->view->advertisersBalance = $advertisersBalance;
        
        $publishersBalance = $statistic->getPublishersBalance();
        $this->view->publishersBalance = $publishersBalance;
        
        $totalBalance = $advertisersBalance['balance'] + $publishersBalance['balance'];
        $this->view->totalBalance = round($totalBalance, 2);
        
        /**
         * Publishers
         */
        $input = 1; $output = 2;
        $banner = 1; $context = 2; $blog = 3; $socialNetwork = 4;
        $video = 5;
        $allPublishers = $statistic->getAllPublishers();
        foreach ($allPublishers as $key => $publisher) {
            //выведено средств
            $allOutputs = $statistic->getSelectPublisherAmountTransactions($publisher['id'], $output);
            $allPublishers[$key]['output'] = round($allOutputs['sum'], 2);
            
            //доход
            $allInputs = $statistic->getSelectPublisherAmountTransactions($publisher['id'], $input);
            $allPublishers[$key]['input'] = round($allInputs['sum'], 2);
            
            $contextAmount = $statistic->getSelectTypeAmountPublisherTransactions($publisher['id'], $context);
            $allPublishers[$key]['context'] = round($contextAmount['sum'], 2);
            
            $bannerAmount = $statistic->getSelectTypeAmountPublisherTransactions($publisher['id'], $banner);
            $allPublishers[$key]['banner'] = round($bannerAmount['sum'], 2);
        }        
        $this->view->allPublishers = $allPublishers;
        
        /**
         * Advertisers
         */
        $allAdvertisers = $statistic->getAllAdvertisers();
        foreach ($allAdvertisers as $key => $advertiser) {
            //введено в систему
            $amountInputs = $statistic->getSelectTransactionsAmountAdvertiser($input, $advertiser['id']);
            $allAdvertisers[$key]['input'] = round($amountInputs['sum'], 2);
            
            //расходы
            $amountOutputs = $statistic->getSelectTransactionsAmountAdvertiser($output, $advertiser['id']);
            $allAdvertisers[$key]['output'] = round($amountOutputs['sum'], 2);
            
            //расходы на баннеры
            $amountOutputsBanner = $statistic->getOutputSelectTypeAmountAdvertiser($advertiser['id'], $banner);
            $allAdvertisers[$key]['output_banners'] = round($amountOutputsBanner['sum'], 2);
            
            //расходы на контекстную рекламу
            $amountOutputsAdver = $statistic->getOutputSelectTypeAmountAdvertiser($advertiser['id'], $context);
            $amountOutputsVideo = $statistic->getOutputSelectTypeAmountAdvertiser($advertiser['id'], $video);
            $allAdvertisers[$key]['output_context'] = round($amountOutputsAdver['sum'] + $amountOutputsVideo['sum'] , 2);
            
            //расходы на соц.сети
            $amountOutputsSocialNetwork = $statistic->getOutputSelectTypeAmountAdvertiser($advertiser['id'], $socialNetwork);
            $allAdvertisers[$key]['output_social'] = round($amountOutputsSocialNetwork['sum'], 2);
            
            //расходны на блоги
            $amountOutputsBlog = $statistic->getOutputSelectTypeAmountAdvertiser($advertiser['id'], $blog);
            $allAdvertisers[$key]['output_blog'] = round($amountOutputsBlog['sum'], 2);     
        }
        
        $this->view->allAdvertisers = $allAdvertisers;
    }
    
    public function websitesstatisticAction() {
        $statistic = new Back_Model_Statistic();
        
        $active = 1; $notConfirmed = 2; $blocked = 3;
        
        /**
         * Websites
         */
        $countSites = $statistic->getCountAllSites();
        $this->view->countSites = $countSites;
        
        $countActiveSites = $statistic->getCountStatusSites($active);
        $this->view->countActiveSites = $countActiveSites;
        
        $countNotConfirmedSites = $statistic->getCountStatusSites($notConfirmed);
        $this->view->countNotConfirmedSites = $countNotConfirmedSites;
        
        $countBlockedSites = $statistic->getCountStatusSites($blocked);
        $this->view->countBlockedSites = $countBlockedSites;

        /**
         * Advertising spaces
         */
        $countAdvSpaces = $statistic->getCountAllAdvSpaces();
        $this->view->countAdvSpaces = $countAdvSpaces;
        
        $countActiveAdvSpaces = $statistic->getCountStatusAdvSpaces($active);
        $this->view->countActiveAdvSpaces = $countActiveAdvSpaces;
        
        $countNotConfirmedAdvSpaces = $statistic->getCountStatusAdvSpaces($notConfirmed);
        $this->view->countNotConfirmedAdvSpaces = $countNotConfirmedAdvSpaces;
        
        $countBlockedAdvSpaces = $statistic->getCountStatusAdvSpaces($blocked);
        $this->view->countBlockedAdvSpaces = $countBlockedAdvSpaces;
    }
    
    public function campaignsstatisticAction() {
        $statistic = new Back_Model_Statistic();
        
        $active = 1; $notConfirmed = 2; $blocked = 3;
        
        /**
         * Campaigns
         */
        $countCampaigns = $statistic->getCountAllCampaigns();
        $this->view->countCampaigns = $countCampaigns;
        
        $countActiveCampaigns = $statistic->getCountStatusCampaigns($active);
        $this->view->countActiveCampaigns = $countActiveCampaigns;
        
        $countNotConfirmedCampaigns = $statistic->getCountStatusCampaigns($notConfirmed);
        $this->view->countNotConfirmedCampaigns = $countNotConfirmedCampaigns;
        
        $countBlockedCampaigns = $statistic->getCountStatusCampaigns($blocked);
        $this->view->countBlockedCampaigns = $countBlockedCampaigns;
        
        /**
         * Advertisements
         */
        $banner = 1; $context = 2; $blog = 3; $socialNetwork = 4;
        $video = 5;
        $countAdvertisements = $statistic->getCountAllAdvertisements();
        $this->view->countAdvertisements = $countAdvertisements;
        
        $countBannerAdvertisements = $statistic->getCountTypeAdvertisements($banner);
        $this->view->countBannerAdvertisements = $countBannerAdvertisements;
        
        $countContextAdvertisements = $statistic->getCountTypeAdvertisements($context);
        $this->view->countContextAdvertisements = $countContextAdvertisements;
        
        $countBlogAdvertisements = $statistic->getCountTypeAdvertisements($blog);
        $this->view->countBlogAdvertisements = $countBlogAdvertisements;
        
        $countSocialNetworkAdvertisements = $statistic->getCountTypeAdvertisements($socialNetwork);
        $this->view->countSocialNetworkAdvertisements = $countSocialNetworkAdvertisements;
        
        $countVideoAdvertisements = $statistic->getCountTypeAdvertisements($video);
        $this->view->countVideoAdvertisements = $countVideoAdvertisements;
        
        /**
         * Advertisers payments
         */
        $allPaymentsAdvertisers = $statistic->getAllAdvPayments();
        $this->view->allPaymentsAdvertisers = $allPaymentsAdvertisers;
                
        $lastMonthPaymentsAdvertisers = $statistic->getLastMonthAdvPayments();
        $this->view->lastMonthPaymentsAdvertisers = $lastMonthPaymentsAdvertisers;
        
        $todayAdvPayments = $statistic->getTodayAdvPayments();
        $this->view->todayAdvPayments = $todayAdvPayments;
        
        $reservetBudgetCampaigns = $statistic->getCurrentBalanceActiveCampaigns();
        $this->view->reservetBudgetCampaigns = $reservetBudgetCampaigns;
    }
}

?>
